// pages/upload/upload.js
const app = getApp()
const util = require('../../utils/util.js');
const api = require('../../utils/api.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // user:'',
        url:'',
        name:'',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getUserInfo();
    },
    getUserInfo:function(){
        var that =this;
        let url = api.userInfo();
        util.http(true, 'GET', url,'null',res=>{
            that.setData({
                name:res.nick_name,
                url: res.avatar
            })
        })
    },
    setPhotoInfo: function () {
        var that = this;
        util.chooseImage(function (res) {
            var tempFilePaths = res.tempFilePaths[0];
            that.setData({
                imgUrl: tempFilePaths
            })
            util.uploadFile(tempFilePaths, function (res) {
                var data = JSON.parse(res.data)
                util.hideToast();
                that.setData({
                    url: data.data.url
                })
                that.up(data.data.url);
            })
        }, 1)
    },
    // 上传
    up:function(a){
        var that =this;
        let url = api.changeavatar();
        util.http(true, 'POST', url,{
            avatar:a
        },res=>{
            console.log(res);
        })  
    },
    userNameInput: function (e) {
        this.setData({
            userName: e.detail.value
        })
    },
    bindnames: function (e) {
        var that = this;
        let url = api.changeNickname();
        if (e.detail.value == "") {
            wx.showModal({
                content: '名字不能为空',
                showCancel:false,
            })
            return;
        };
        util.http(true, "POST", url,{
            nick_name: e.detail.value
        },res=>{
            that.setData({
                name: e.detail.value
            })
            console.log(res);
        })
        //console.log(e.detail.value);
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        util.setNavigationBarTitle("个人资料");
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})